
package org.xs.object.dao.user;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.awt.print.Pageable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

public interface UserRepository extends JpaRepository<BaseUser, Integer> {
    //查询
    Optional<BaseUser> findByUsername(String username);

    Optional<BaseUser> findById(Integer id);
    //原生的sql语句查询
    //查询用户头像
    @Query(value = "select u.user_pic from BaseUser u where u.id=?1")
    String findByUserPic(Integer userId);
    //查询用户邮箱
    @Query(value = "select u.email from BaseUser u where u.id=?1")
    String findByEmail(Integer userId);
    //查询用户昵称
    @Query(value = "select u.nickname from BaseUser u where u.id=?1")
    String findByNickname(Integer userId);
    //修改用户数据
    @Modifying
    @Query(value = "update BaseUser u set u.nickname=?1,u.email=?2,u.update_time=?3 where u.id=?4")
    void updateUser(String nickname, String email, LocalDateTime update_time, Integer userId);

    @Modifying
    @Query(value = "update BaseUser u set u.user_pic=?1,u.update_time=?2 where u.id=?3")
    void updateAvater(String avatarUrl, LocalDateTime now, Integer userId);
    //查询用户列表
    @Query(value = "select u from BaseUser u")
    List<BaseUser> findAllBuUser(Pageable pageable);
}
